<?php

namespace app\admin\model;

use think\Model;
use think\model\concern\SoftDelete;

class Comment extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $defaultSoftDelete = 0;
    public function article()
    {
        return $this->hasOne(Article::class, 'id', 'art_id');
    }
    public function userProfile()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }
    public static function list()
    {
        $limit = input('limit');
        $comment = input('comment');
        $id = input('id');
        $uid = input('user_id');
        $list = self::alias('a')
            ->leftJoin('comment b', 'a.pid = b.id')
            ->field('a.*, b.comment as parent_name');
        if (!empty($comment)) {
            $list->where('a.comment', 'like', "%$comment%");
        }
        if (!empty($id)) {
            $list->where('a.id', $id);
        }
        if (!empty($uid)) {
            $list->where('a.user_id', $uid);
        }
        $list = $list->paginate($limit);
        foreach ($list as &$com) {
            if ($com['pid'] > 0) {
                $list['comment'] = '|—' . $com['comment'];
            }
        }
        return $list;
    }
}
